package org.open.test.queue.util;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import com.alibaba.fastjson.JSON;

public class ExecutorServiceUtil {

	public static boolean running = false;

	private static Runnable command = new Runnable() {

		private MessageQueue messageQueue = MessageQueue.getInstance();

		@Override
		public void run() {
			try {
				while (running && !messageQueue.isEmpty()) {
					System.out.println("已发送:" + JSON.toJSONString(messageQueue.poll()));
					Thread.sleep(5000);
				}
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	};

	private final static ExecutorService EXECUTOR_SERVICE = Executors.newSingleThreadExecutor();

	public static void sendMsg() {
		EXECUTOR_SERVICE.execute(command);
	}

	public static void stop() {
		running = false;
	}
	
	public static void start() {
		running = true;
		sendMsg();
	}

}
